package com.example.gybicycle.common.Log;


import com.example.gybicycle.common.util.LoginUtil;
import com.example.gybicycle.entity.SysLog;
import com.example.gybicycle.entity.SysUser;
import com.example.gybicycle.service.SysLogService;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Objects;

@Aspect
@Component
public class SysLogAspect {
    @Autowired
    private SysLogService logService;
    /**
     * 这里我们使用注解的形式
     * 当然，我们也可以通过切点表达式直接指定需要拦截的package,需要拦截的class 以及 method
     * 切点表达式:   execution(...)
     */
    @Pointcut("@annotation(SysLoging)")
//    @Pointcut("execution(* com.example.gybicycle.service.*.*(..))")
    public void logPointCut() {}
    /**
     * 环绕通知 @Around  ， 当然也可以使用 @Before (前置通知)  @After (后置通知)
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        Object result = point.proceed();
        long time = System.currentTimeMillis() - beginTime;
        try {
            saveLog(point, time);
        } catch (Exception e) {
        }
        return result;
    }

    /**
     * 保存日志
     * @param joinPoint
     * @param time
     */
    private void saveLog(ProceedingJoinPoint joinPoint, long time) {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
//        String ip= IpUtil.getIpAddr(request);
//        String ip= IpUtil.getLocalIp(request);
        SysUser user= LoginUtil.getLoginInfo(request).getUser();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLog log = new SysLog();
        log.setCreateTime(new Date());
        SysLoging loging = method.getAnnotation(SysLoging.class);
        String logvalue="";
        if(loging != null){
            //注解上的描述
            logvalue=loging.value();
        }
        //请求的 类名、方法名
//        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();

        try{

            if(!methodName.equals("isLogin")&&!methodName.equals("autoSave")){
                log.setMode(logvalue);
//                log.setCreateIp(ip);
//                log.setCreateArea(IpUtil.getCountry(ip));
                if(user!=null)
                    log.setCreateBy(user.getId());
                if(log.getStatus()==null)
                    log.setStatus(0);
                log.setAction(methodName);
                logService.insert(log);
            }

        }catch (Exception e){
            e.printStackTrace();
        }


    }

}
